package com.crowdsourcing.service.impl;

import com.crowdsourcing.dao.TaskMapper;
import com.crowdsourcing.model.Task;
import com.crowdsourcing.model.TaskExample;
import com.crowdsourcing.service.ITaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class TaskServiceImpl implements ITaskService {
    @Autowired
    TaskMapper taskMapper;

    @Override
    public void save(String content, Integer userId, String title) {
        Task task = new Task();
        task.setContent(content);
        task.setUserId(userId);
        task.setTime(new Date());
        task.setTitle(title);
        taskMapper.insert(task);
    }

    @Override
    public List<Task> all(Integer userId) {
        TaskExample taskExample = new TaskExample();
        TaskExample.Criteria criteria = taskExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        return taskMapper.allSelectByExample(taskExample);
    }


    @Override
    public void handle(String content, Integer taskId, Integer handleId) {
        Task task = taskMapper.selectByPrimaryKey(taskId);
        task.setHandleContent(content);
        task.sethId(handleId);
        taskMapper.updateByPrimaryKey(task);
    }


    @Override
    public List<Task> receive(Integer userId) {
        TaskExample taskExample = new TaskExample();
        TaskExample.Criteria criteria = taskExample.createCriteria();
        criteria.andHIdEqualTo(userId);
        return taskMapper.selectByExample(taskExample);

    }

    @Override
    public List<Task> receiveFinish(Integer userId) {
        TaskExample taskExample = new TaskExample();
        TaskExample.Criteria criteria = taskExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andHandleContentIsNotNull();
        return taskMapper.allSelectByExample(taskExample);

    }

    @Override
    public List<Task> receiveNoFinish(Integer userId) {
        TaskExample taskExample = new TaskExample();
        TaskExample.Criteria criteria = taskExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andHandleContentIsNull();
        return taskMapper.allSelectByExample(taskExample);
    }


    @Override
    public List<Task> publisher(Integer handleId) {
        TaskExample taskExample = new TaskExample();
        TaskExample.Criteria criteria = taskExample.createCriteria();
        criteria.andUserIdEqualTo(handleId);
        return taskMapper.selectByExample(taskExample);
    }

    @Override
    public List<Task> allReceive() {
        TaskExample taskExample = new TaskExample();
        TaskExample.Criteria criteria = taskExample.createCriteria();
        criteria.andHandleContentIsNull();
        return taskMapper.allSelectByExample(taskExample);
    }

    @Override
    public List<Task> finishReceive(Integer id) {
        TaskExample taskExample = new TaskExample();
        TaskExample.Criteria criteria = taskExample.createCriteria();
        criteria.andHIdEqualTo(id);
        return taskMapper.allSelectByExample(taskExample);
    }

    @Override
    public void saveReceive(Integer taskId, String content, Integer id) {
        Task task = taskMapper.selectByPrimaryKey(taskId);
        if (task != null) {
            task.setHandleContent(content);
            task.sethId(id);
            taskMapper.updateByPrimaryKey(task);
        }
    }

    @Override
    public Task find(Integer taskId) {
        return taskMapper.selectByPrimaryKey(taskId);
    }
}
